package mutation;


import ga.Individual;

import java.util.LinkedList;
import java.util.List;

import model.StatementKind;
import operations.CreateObject;
import operations.InvokeMethod;
import operations.Operation;
import operations.ValuesGenerator;
import util.DebugConfig;
import util.Log;


public class RandomDeleteMethodCall extends MutationStrategy {

	@Override
	public Individual mutate(Individual individual) {
		if(DebugConfig.deleteRandomMethodClass){
			Log.log("-------  Delete Random Method Call Mutation ---------------"); 
			Log.log("--------------------individual1----------------------");
			Log.log(individual.toString()+ " size:"+ individual.getSize());
		}

		Individual mutatedIndividual=individual.clone();
		List<Operation> ops =mutatedIndividual.getStatements();
		try {
			List<StatementKind> methodList= new LinkedList<StatementKind>();
//			methodList.removeAll(Collections.singleton(st));

			List<Operation> newOps = new LinkedList<Operation>();
	
			//remove operations from the individual
			//int num = Randomness.nextRandomInt(10);
			for(Operation op : ops) {
				if (getMutationFlag(ops.size()))
					continue;
					if (op instanceof CreateObject){
						 if(methodList.contains(((CreateObject)op).getConstructor()))
							 	newOps.add(op);
					}
					else if(op instanceof InvokeMethod){
						 if(methodList.contains(((InvokeMethod)op).getMethod()))
							 	newOps.add(op);
						 }
				}
			if(newOps.size()!=0){
				ops.clear();
				ops.addAll(newOps);
			}

			return mutatedIndividual;
		} catch (Throwable e){
			System.err.println("Delete Random Method Call Class:" + e.getMessage());
		}	
		return null;
		
	}

	@Override
	public boolean getMutationFlag(int size) {
		if(ValuesGenerator.getBoolean(1.0f/size))
			return true;
		return false;

	}
}

